package com.sneaker.shower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.sneaker.shower.common.BizException;
import com.sneaker.shower.common.ThreadLocalCache;
import com.sneaker.shower.entity.domain.user.UserReceiveInfoDO;
import com.sneaker.shower.entity.dto.UserReceiveInfoDTO;
import com.sneaker.shower.mapper.UserReceiveInfoMapper;
import com.sneaker.shower.mapstruct.UserReceiveMapstruct;
import com.sneaker.shower.service.UserReceiveInfoService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class UserReceiveInfoServiceImpl extends ServiceImpl<UserReceiveInfoMapper, UserReceiveInfoDO> implements UserReceiveInfoService {

    @Override
    public List<UserReceiveInfoDTO> listAll() {
        long userGuid = Long.parseLong(ThreadLocalCache.get().getGuid());
        List<UserReceiveInfoDO> list = list(new LambdaQueryWrapper<UserReceiveInfoDO>().eq(UserReceiveInfoDO::getUserGuid, userGuid));
        if (CollectionUtils.isEmpty(list)) {
            return Lists.newArrayList();
        }
        return UserReceiveMapstruct.INSTANCE.doList2DTOList(list);
    }

    @Override
    public UserReceiveInfoDTO getInfo(String guid) {
        UserReceiveInfoDO byId = getById(guid);
        return UserReceiveMapstruct.INSTANCE.do2DTO(byId);
    }

    @Override
    public Boolean addReceive(UserReceiveInfoDTO userReceiveInfoDTO) {
        checkReceiveParams(userReceiveInfoDTO);
        UserReceiveInfoDO userReceiveInfoDO = UserReceiveMapstruct.INSTANCE.dto2DO(userReceiveInfoDTO);
        userReceiveInfoDO.setUserGuid(Long.parseLong(ThreadLocalCache.get().getGuid()));
        if (!StringUtils.isEmpty(userReceiveInfoDTO.getGuid())) {
            userReceiveInfoDO.setGuid(Long.parseLong(userReceiveInfoDTO.getGuid()));
        } else {
            userReceiveInfoDO.setGuid(IdWorker.getId());
        }
        return saveOrUpdate(userReceiveInfoDO);
    }

    @Override
    public Boolean updateReceive(UserReceiveInfoDTO userReceiveInfoDTO) {
        checkReceiveParams(userReceiveInfoDTO);
        UserReceiveInfoDO entity = UserReceiveMapstruct.INSTANCE.dto2DO(userReceiveInfoDTO);
        if (StringUtils.isEmpty(userReceiveInfoDTO.getGuid())) {
            entity.setGuid(Long.parseLong(userReceiveInfoDTO.getGuid()));
            updateById(entity);
        }
        return true;
    }

    @Override
    public Boolean delReceive(String guid) {
        return removeById(guid);
    }

    private void checkReceiveParams(UserReceiveInfoDTO dto) {
        if (StringUtils.isEmpty(dto.getName())) {
            throw new BizException("收货人不能为空");
        }
        if (StringUtils.isEmpty(dto.getTel())) {
            throw new BizException("联系电话不能为空");
        }
        if (StringUtils.isEmpty(dto.getAddress())) {
            throw new BizException("收货地址不能为空");
        }
        if (dto.getTel().length() > 20) {
            throw new BizException("请输入正确的联系电话");
        }

        if (dto.getName().length() > 20) {
            throw new BizException("收货人最多支持20位字符");
        }
    }
}
